package com.example.demo.config;

import com.example.demo.filters.CircuitBreakerFilter;
import com.example.demo.filters.RateLimiterFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.ZonedDateTime;

/**
 * 网关配置
 */
@Configuration
public class GatewayConfig {

//    @Autowired
//    private RateLimiterFilter rateLimiterFilter;
//
//    @Autowired
//    private CircuitBreakerFilter circuitBreakerFilter;

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        ZonedDateTime startTime = ZonedDateTime.now();
        ZonedDateTime endTime = startTime.plusSeconds(10);
        System.out.println(startTime);
        System.out.println(endTime);

        return builder.routes()
                .route("user-service", r -> r.path("/user/**")
                        .filters(f -> {
                            f.addRequestHeader("id", "1123");
                            f.addResponseHeader("response-id", "test");
                            // 移除Hystrix，添加CircuitBreakerFilter
//                            f.filter(circuitBreakerFilter);//断路过滤器
//                            f.filter(rateLimiterFilter);//限流过滤器
                            return f;
                        })
                        .uri("lb://user")
                ).build();
    }
}
